package com.pansky.crm.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pansky.crm.annotation.OperateLog;
import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.entity.Department;
import com.pansky.crm.entity.vo.DepartmentVO;
import com.pansky.crm.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/2/26 9:27
 * @description：部门controller
 */
@RestController
@RequestMapping("/department")
@OperateLog
@Api(tags = {"管理端-部门api"})
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "查询部门及员工列表", notes = "查询部门及员工列表")
    @GetMapping("/listEmployee")
    public ApiResult<List<DepartmentVO>> listDepartmentEmployee() {
        return departmentService.listAllDepartmentEmployee(null);
    }

    @ApiOperation(value = "根据部门名称查询部门及员工列表", notes = "根据部门名称查询部门及员工列表")
    @GetMapping("/listEmployeeByDepartmentName")
    public ApiResult<List<DepartmentVO>> listDepartmentEmployee(@RequestParam String departmentName) {
        return departmentService.listAllDepartmentEmployee(departmentName);
    }
    @ApiOperation(value = "添加部门", notes = "添加部门")
    @PostMapping("/add")
    public ApiResult<Boolean> addDepartment(@Valid @RequestBody Department departmentCreateDTO) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id").last("limit 1");
        Department department = departmentService.getOne(queryWrapper);
        departmentCreateDTO.setSort(department.getId() + 1);
        return ApiResult.ok(departmentService.save(departmentCreateDTO));
    }

    @ApiOperation(value = "更新部门信息", notes = "更新部门信息")
    @PostMapping("/update")
    public ApiResult<Boolean> updateDepartment(@Valid @RequestBody Department departmentUpdateDTO) {
        return ApiResult.ok(departmentService.updateById(departmentUpdateDTO));
    }

    @ApiOperation(value = "删除部门", notes = "删除部门")
    @PostMapping("/delete")
    public ApiResult<Boolean> delDepartment(@RequestParam Integer departmentId) {

        return departmentService.removeByDepartmentId(departmentId);
    }

//    @ApiOperation(value = "获取部门信息", notes = "获取部门")
//    @GetMapping("/query/{departmentId}")
//    public ApiResult<DepartmentVO> getDepartment(@PathVariable("departmentId") Integer departmentId) {
//        return departmentService.getDepartmentById(departmentId);
//    }

//    @ApiOperation(value = "查询部门列表", notes = "查询部门列表")
//    @GetMapping("/listAll")
//    public ApiResult<List<DepartmentVO>> listAll() {
//        return departmentService.listAll();
//    }


    @ApiOperation(value = "上下移动")
    @GetMapping("/upOrDown")
    public ApiResult<Boolean> upOrDown(@RequestParam Integer departmentId,@RequestParam Integer swapId) {
        return departmentService.upOrDown(departmentId,swapId);
    }

    @ApiOperation(value = "升级")
    @GetMapping("/upgrade")
    public ApiResult<Boolean> upgrade(@RequestParam Integer departmentId) {
        return departmentService.upgrade(departmentId);
    }

    @ApiOperation(value = "降级")
    @GetMapping("/downgrade")
    public ApiResult<Boolean> downgrade(@RequestParam Integer departmentId,@RequestParam Integer preId) {
        return departmentService.downgrade(departmentId,preId);
    }
}
